package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import org.testng.annotations.DataProvider;
import com.example.fw.ApplicationManager;
import com.example.tests.ContactObject;

public class ContactDataGenerator {
       
        Random rnd = new Random();
        private static ApplicationManager app;
       
        public static void main(String[] args) throws Exception {
                if (args.length < 2) {
                        System.out.println("Specify two parameters: fileName and contactsCount");
                        return;
                }
                String file = args[0];
                int count = Integer.parseInt(args[1]);
               
                app = new ApplicationManager();
                Properties props = new Properties();
                props.load(new FileReader("application.properties"));
                app.setProperties(props);
               
                try {
                        new ContactDataGenerator().generateDataToFile(file, count);
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
       
        @DataProvider(name = "randomContacts")
        public static Iterator<Object[]> generateRandomContacts() throws Exception {
                List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(5);
                List<Object[]> list = new ArrayList<Object[]>();
                for (ContactObject contact : contacts) {
                        list.add(new Object[]{contact});
                }
                return list.iterator();
        }
       
        @DataProvider(name = "contactsFromFile")
        public static Iterator<Object[]> loadContactsFromFile() throws IOException {
               
                List<Object[]> list = new ArrayList<Object[]>();
                String fileName = app.getInstance().getProperty("contactFile");
                BufferedReader reader = new BufferedReader(new FileReader(fileName));
                String line = reader.readLine();
                while (line != null) {
                        String[] parts = line.split("\t");
                        ContactObject contact = new ContactObject()
                        .setFirstName(parts[0])
                        .setLastName(parts[1])
                        .setAddress(parts[2])
                        .setHome(parts[3])
                        .setMobilePhone(parts[4])
                        .setWorkPhone(parts[5])
                        .setEmail(parts[6])
                        .setBirthDay(parts[7])
                        .setBirthMonth(parts[8])
                        .setByear(parts[9])
                        .setSelectGroup(parts[10])
                        .setAddress2(parts[12])
                        .setHomePhone(parts[13]);
                        list.add(new Object[]{contact});
                        line = reader.readLine();
                }
                return list.iterator();
        }
       
        private void generateDataToFile(String fileName, int count) throws Exception {          
                List<ContactObject> contacts = generateRandomContactList(count);
                File file = new File(fileName);
                if (file.exists()) {
                        file.delete();
                }
                FileWriter writer = new FileWriter(file);
                for (ContactObject contact : contacts) {
                        writer.write(contact.firstName + "\t" + contact.lastName + "\t" 
                        			+ contact.address  + "\t" + contact.home + "\t"
                                    + contact.mobilePhone + "\t" + contact.workPhone + "\t"
                                    + contact.email + "\t" + contact.birthDay + "\t"
                                    + contact.birthMonth + "\t" + contact.byear + "\t"
                                    + contact.selectGroup + "\t" + contact.address2 + "\t"
                                    + contact.homePhone + "\t" + "\n");
                }
                writer.close();
                app.stop();
        }
       
        private List<ContactObject> generateRandomContactList(int count) throws Exception {    
        	List<ContactObject> list = new ArrayList<ContactObject>();
                for (int i =0; i<count; i++) {
                	ContactObject group = new ContactObject()
    				.setFirstName("firstName"+rnd.nextInt())
    				.setLastName("lastName"+rnd.nextInt())
    				.setAddress("address"+rnd.nextInt())
    				.setHome("home"+rnd.nextInt())
    				.setMobilePhone("mobilePhone"+rnd.nextInt())
    				.setWorkPhone("workPhone"+rnd.nextInt())
    				.setEmail("em@il" + rnd.nextInt() + ".com")
    				.setByear("byear"+rnd.nextInt())
    				.setAddress2("address2"+rnd.nextInt())
    				.setHomePhone("homePhone"+rnd.nextInt())
    				.setBirthDay("birthDay"+rnd.nextInt())
    				.setBirthMonth(getRandomMonth())
    				.setSelectGroup("selectGroup"+rnd.nextInt());
				list.add(group);
    		}
    		return list;
        }
       
        String[] MONTHS = new String[] { "January", "February",
                        "March", "April", "May", "June", "July", "August", "September",
                        "October", "November", "December" };
       
        public int generateRandomNumberBetween(int start, int end) {
                int interval = end - start;
                int out = new Random().nextInt(Math.abs(interval));
                return interval>0?(out + start):(out + end);
        }
       
        public String getRandomMonth() {
                return MONTHS[rnd.nextInt(12)];
        }
       
        public String getRandomDay(int daysInMonth) {
                return Integer.toString((generateRandomNumberBetween(1, daysInMonth)));
        }
       
        public String getRandomYear(int startYear, int endYear) {
                return Integer.toString((generateRandomNumberBetween(startYear, endYear)));
        }

}
